﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AG.Data;
using System.Drawing;
using AG.MapEditor.Data;
using System.Windows.Forms;

namespace AGMapEditor.Controls
{
    public partial class AGEMapPanel
    {
        private void OnRenderDesignBlockMode(Graphics graphics, PaintEventArgs e)
        {
            for (ushort rowIndex = 0; rowIndex < _map.Row; rowIndex++)
            {
                for (ushort colIndex = 0; colIndex < _map.Col; colIndex++)
                {
                    PointF pt = MapPosToMapPoint(rowIndex, colIndex);
                    //e.Graphics.DrawRectangle(Pens.Green, pt.X, pt.Y, TW, TH);
                    pt.Y += _originOffsetY;

                    PointF pt2 = new PointF(pt.X, pt.Y - TH / 2);

                    Rectangle tileRect = new Rectangle((int)pt2.X, (int)pt2.Y, (int)TW, (int)TH);
                    if (e.ClipRectangle.IntersectsWith(tileRect))
                    {
                        AGMapTile tile = _map.GetTile(rowIndex, colIndex);
                        AGTexture texture = GetTileTexture(tile);
                        graphics.DrawImage(texture.Bitmap, pt2.X, pt2.Y, texture.Size.Width, texture.Size.Height);
                    }
                }
            }

            for (int index = 0; index < _map.ObjectList.Count; index++)
            {
                AGMapObject mapObject = _map.ObjectList[index];
                PointF tileCenterPoint = MapPosToUIPoint(mapObject.PosRow, mapObject.PosCol);
                tileCenterPoint.X += TW / 2;

                AGModel model = GetUnitModel(mapObject.UnitId);
                AGModelFrame frame = model.GetAction(0).GetDirection(0).GetFrame(0);

                Rectangle destRect = new Rectangle(
                    (int)tileCenterPoint.X - frame.Pivot.X,
                    (int)tileCenterPoint.Y - frame.Pivot.Y,
                    frame.SrcRect.Width,
                    frame.SrcRect.Height);

                if (e.ClipRectangle.IntersectsWith(destRect))
                {
                    AGTexture texture = AGMEContext.Current.Resource.GetTexture(frame.Texture);
                    graphics.DrawImage(texture.Bitmap,
                        tileCenterPoint.X - frame.Pivot.X,
                        tileCenterPoint.Y - frame.Pivot.Y,
                        texture.Size.Width,
                        texture.Size.Height);
                    if (this.SelectedObject == mapObject)
                    {
                        graphics.DrawRectangle(Pens.Red, destRect);
                    }
                }
            }

            for (ushort rowIndex = 0; rowIndex < _map.Row; rowIndex++)
            {
                for (ushort colIndex = 0; colIndex < _map.Col; colIndex++)
                {
                    PointF pt = MapPosToMapPoint(rowIndex, colIndex);
                    //e.Graphics.DrawRectangle(Pens.Green, pt.X, pt.Y, TW, TH);
                    pt.Y += _originOffsetY;

                    PointF pt2 = new PointF(pt.X, pt.Y - TH / 2);

                    Rectangle tileRect = new Rectangle((int)pt2.X, (int)pt2.Y, (int)TW, (int)TH);
                    if (e.ClipRectangle.IntersectsWith(tileRect))
                    {
                        AGMapTile tile = _map.GetTile(rowIndex, colIndex);
                        if (tile.IsBlock)
                        {
                            graphics.DrawLine(Pens.Red, pt.X, pt.Y, pt.X + TW / 2, pt.Y - TH / 2);
                            graphics.DrawLine(Pens.Red, pt.X + TW / 2, pt.Y - TH / 2, pt.X + TW, pt.Y);
                            graphics.DrawLine(Pens.Red, pt.X + TW, pt.Y, pt.X + TW / 2, pt.Y + TH / 2);
                            graphics.DrawLine(Pens.Red, pt.X + TW / 2, pt.Y + TH / 2, pt.X, pt.Y);

                            graphics.DrawLine(Pens.Red, pt.X, pt.Y, pt.X + TW, pt.Y);
                            graphics.DrawLine(Pens.Red, pt.X + TW / 2, pt.Y - TH / 2, pt.X + TW / 2, pt.Y + TH / 2);
                        }
                        else
                        {
                            graphics.DrawLine(Pens.Green, pt.X, pt.Y, pt.X + TW / 2, pt.Y - TH / 2);
                            graphics.DrawLine(Pens.Green, pt.X + TW / 2, pt.Y - TH / 2, pt.X + TW, pt.Y);
                            graphics.DrawLine(Pens.Green, pt.X + TW, pt.Y, pt.X + TW / 2, pt.Y + TH / 2);
                            graphics.DrawLine(Pens.Green, pt.X + TW / 2, pt.Y + TH / 2, pt.X, pt.Y);
                        }
                    }
                }
            }
        }

        private void OnRenderEditObjectMode(Graphics graphics, PaintEventArgs e)
        {
            for (ushort rowIndex = 0; rowIndex < _map.Row; rowIndex++)
            {
                for (ushort colIndex = 0; colIndex < _map.Col; colIndex++)
                {
                    PointF pt = MapPosToMapPoint(rowIndex, colIndex);
                    //e.Graphics.DrawRectangle(Pens.Green, pt.X, pt.Y, TW, TH);
                    pt.Y += _originOffsetY;

                    PointF pt2 = new PointF(pt.X, pt.Y - TH / 2);

                    Rectangle tileRect = new Rectangle((int)pt2.X, (int)pt2.Y, (int)TW, (int)TH);
                    if (e.ClipRectangle.IntersectsWith(tileRect))
                    {
                        AGMapTile tile = _map.GetTile(rowIndex, colIndex);
                        AGTexture texture = GetTileTexture(tile);
                        graphics.DrawImage(texture.Bitmap, pt2.X, pt2.Y, texture.Size.Width, texture.Size.Height);

                        graphics.DrawLine(Pens.Green, pt.X, pt.Y, pt.X + TW / 2, pt.Y - TH / 2);
                        graphics.DrawLine(Pens.Green, pt.X + TW / 2, pt.Y - TH / 2, pt.X + TW, pt.Y);
                        graphics.DrawLine(Pens.Green, pt.X + TW, pt.Y, pt.X + TW / 2, pt.Y + TH / 2);
                        graphics.DrawLine(Pens.Green, pt.X + TW / 2, pt.Y + TH / 2, pt.X, pt.Y);
                    }
                }
            }

            for (int index = 0; index < _map.ObjectList.Count; index++)
            {
                AGMapObject mapObject = _map.ObjectList[index];
                PointF tileCenterPoint = MapPosToUIPoint(mapObject.PosRow, mapObject.PosCol);
                tileCenterPoint.X += TW / 2;

                AGModel model = GetUnitModel(mapObject.UnitId);
                AGModelFrame frame = model.GetAction(0).GetDirection(0).GetFrame(0);

                Rectangle destRect = new Rectangle(
                    (int)tileCenterPoint.X - frame.Pivot.X,
                    (int)tileCenterPoint.Y - frame.Pivot.Y,
                    frame.SrcRect.Width,
                    frame.SrcRect.Height);

                if (e.ClipRectangle.IntersectsWith(destRect))
                {
                    AGTexture texture = AGMEContext.Current.Resource.GetTexture(frame.Texture);
                    graphics.DrawImage(texture.Bitmap,
                        tileCenterPoint.X - frame.Pivot.X,
                        tileCenterPoint.Y - frame.Pivot.Y,
                        texture.Size.Width,
                        texture.Size.Height);
                    if (this.SelectedObject == mapObject)
                    {
                        graphics.DrawRectangle(Pens.Red, destRect);
                    }
                }
            }
        }

        private void OnRenderSetObjectMode(Graphics graphics, PaintEventArgs e)
        {
            for (ushort rowIndex = 0; rowIndex < _map.Row; rowIndex++)
            {
                for (ushort colIndex = 0; colIndex < _map.Col; colIndex++)
                {
                    PointF pt = MapPosToMapPoint(rowIndex, colIndex);
                    //e.Graphics.DrawRectangle(Pens.Green, pt.X, pt.Y, TW, TH);
                    pt.Y += _originOffsetY;

                    PointF pt2 = new PointF(pt.X, pt.Y - TH / 2);

                    Rectangle tileRect = new Rectangle((int)pt2.X, (int)pt2.Y, (int)TW, (int)TH);
                    if (e.ClipRectangle.IntersectsWith(tileRect))
                    {
                        AGMapTile tile = _map.GetTile(rowIndex, colIndex);
                        AGTexture texture = GetTileTexture(tile);
                        graphics.DrawImage(texture.Bitmap, pt2.X, pt2.Y, texture.Size.Width, texture.Size.Height);
                    }
                }
            }

            for (int index = 0; index < _map.ObjectList.Count; index++)
            {
                AGMapObject mapObject = _map.ObjectList[index];
                PointF tileCenterPoint = MapPosToUIPoint(mapObject.PosRow, mapObject.PosCol);
                tileCenterPoint.X += TW / 2;

                AGModel model = GetUnitModel(mapObject.UnitId);
                AGModelFrame frame = model.GetAction(0).GetDirection(0).GetFrame(0);

                Rectangle destRect = new Rectangle(
                    (int)tileCenterPoint.X - frame.Pivot.X,
                    (int)tileCenterPoint.Y - frame.Pivot.Y,
                    frame.SrcRect.Width,
                    frame.SrcRect.Height);

                if (e.ClipRectangle.IntersectsWith(destRect))
                {
                    AGTexture texture = AGMEContext.Current.Resource.GetTexture(frame.Texture);
                    graphics.DrawImage(texture.Bitmap,
                        tileCenterPoint.X - frame.Pivot.X,
                        tileCenterPoint.Y - frame.Pivot.Y,
                        texture.Size.Width,
                        texture.Size.Height);
                    if (this.SelectedObject == mapObject)
                    {
                        graphics.DrawRectangle(Pens.Red, destRect);
                    }
                }
            }

            for (ushort rowIndex = 0; rowIndex < _map.Row; rowIndex++)
            {
                for (ushort colIndex = 0; colIndex < _map.Col; colIndex++)
                {
                    PointF pt = MapPosToMapPoint(rowIndex, colIndex);
                    //e.Graphics.DrawRectangle(Pens.Green, pt.X, pt.Y, TW, TH);
                    pt.Y += _originOffsetY;

                    PointF pt2 = new PointF(pt.X, pt.Y - TH / 2);

                    Rectangle tileRect = new Rectangle((int)pt2.X, (int)pt2.Y, (int)TW, (int)TH);
                    if (e.ClipRectangle.IntersectsWith(tileRect))
                    {
                        AGMapTile tile = _map.GetTile(rowIndex, colIndex);
                        graphics.DrawLine(Pens.Green, pt.X, pt.Y, pt.X + TW / 2, pt.Y - TH / 2);
                        graphics.DrawLine(Pens.Green, pt.X + TW / 2, pt.Y - TH / 2, pt.X + TW, pt.Y);
                        graphics.DrawLine(Pens.Green, pt.X + TW, pt.Y, pt.X + TW / 2, pt.Y + TH / 2);
                        graphics.DrawLine(Pens.Green, pt.X + TW / 2, pt.Y + TH / 2, pt.X, pt.Y);
                    }
                }
            }

            if (CurUnit != null)
            {
                PointF tileCenterPoint = MapPosToUIPoint(CurPos.X, CurPos.Y);
                tileCenterPoint.X += TW / 2;

                AGModel model = GetUnitModel(CurUnit.Id);
                AGModelFrame frame = model.GetAction(0).GetDirection(0).GetFrame(0);

                Rectangle destRect = new Rectangle(
                    (int)tileCenterPoint.X - frame.Pivot.X,
                    (int)tileCenterPoint.Y - frame.Pivot.Y,
                    frame.SrcRect.Width,
                    frame.SrcRect.Height);
                AGTexture texture = AGMEContext.Current.Resource.GetTexture(frame.Texture);
                graphics.DrawImage(texture.Bitmap,
                    tileCenterPoint.X - frame.Pivot.X,
                    tileCenterPoint.Y - frame.Pivot.Y,
                    texture.Size.Width,
                    texture.Size.Height);
            }
        }
    }
}
